######################################################
#CCES Analysis.
# When Women Run, Voters Will Follow (Sometimes): 
##Examining the Mobilizing Effect of Female Candidates in the 2014 and 2018 Midterm Elections 
#By Safarpour, Wyckoff Gaynor, Rouse, and Swers #
######################################################

#clean the environment.
rm(list=ls()) 

#Packages.
library(haven)
library(foreign) 
library(survey) 
library(tidyverse)
library(readr)
library(margins) 
library(stargazer)

#Setwd.
setwd("/Users/ACS/Dropbox/When Women Run/Revision_PoliticalBehavior/R&R Part 2/Publication Docs/Replication Data and Code/")

#Data.
cces_PA18<-read.csv("cces_PA18.csv", 
              header = TRUE, stringsAsFactors = F)
cces_PA14<-read.csv("cces_PA14.csv", 
                    header = TRUE, stringsAsFactors = F)
cces_WA18<-read.csv("cces_WA18.csv", 
                    header = TRUE, stringsAsFactors = F)
cces_WA14<-read.csv("cces_WA14.csv", 
                    header = TRUE, stringsAsFactors = F)


#WA 2018 General.
#Run as turnout among registered and use the registered weight (which should approximate the voter file).
table(cces_WA18$vv_regstatus, exclude=NULL) #only want to keep those who are registered, so drop vv_regstatus=2 and 3 (no record and unregistered)
summary(cces_WA18$rvweight) #registered voter weight. The weight automatically drops all those who are not registered (weights them as 0)

m1 <-
  glm(
    Voted ~  FemaleCandidate+   FemaleCandidate*Female+
      FemaleCandidate*GenZ+
      FemaleCandidate*Millennial+
      FemaleCandidate*GenX+
      FemaleCandidate*SilentGen+
      PriorVoting + Female + GenZ + Millennial +
      GenX + SilentGen+trumpvote2016,
    weights = rvweight, family=binomial(link="logit"),
    data = cces_WA18
  )
summary(m1)
#Compute Effects of Female Candidate.
summary(margins(m1, variables=c("FemaleCandidate"))) #-0.0180  CIs:-0.5224 0.4863  (n.s.)


#WA 2014 General.
#Note: the rvweight (registered voter weight) is not available for 2014. 
#In lieu of this, we drop those who are either no record or unregistered to more closely approximate the turnout among registered data.
table(cces_WA14$vv_regstatus, exclude=NULL) #only want to keep those who are registered, so drop 2 and 3 (no record and unregistered)

m2 <-
  glm(
    Voted ~  FemaleCandidate+   FemaleCandidate*Female+
      FemaleCandidate*Millennial+
      FemaleCandidate*GenX+
      FemaleCandidate*SilentGen+
      PriorVoting + Female  + Millennial +
      GenX + SilentGen+obamavote2012,
    weights = weight, family=binomial(link="logit"),
    data =subset(cces_WA14, (cces_WA14$vv_regstatus!=2 &cces_WA14$vv_regstatus!=3))
  )
summary(m2)


#Compute Effects of Female Candidate.
summary(margins(m2, variables=c("FemaleCandidate"))) #0.0175  CIs:-0.0288 0.0639  (n.s.) p=0.4585
summary(margins(m2, variables=c("FemaleCandidate"), at=list(Female=0:1)))

#Export model results to table (Appendix Table E.2).
stargazer(m1, m2, out="WAGeneralTurnoutAmongRegisteredCCESComparison.html",
          type="html",
          covariate.labels = c("Female Candidate", "Female", 
                               "Generation Z","Millennials", "Generation X","Silent Generation", 
                               "Prior Voting (2016/2012 General Election)", 
                               "County Trump Vote 2016","County Obama Vote 2012", 
                               "Female Candidate*Female",
                               "Female Candidate*Generation Z",
                               "Female Candidate*Millennials",
                               "Female Candidate*Generation X",
                               "Female Candidate*Silent Generation"),
          dep.var.labels="", column.labels = c("Voted in 2018 Midterms", "Voted in 2014 Midterms"),
          title = "",
          notes        = "Results from logistic regression. Standard errors in parentheses. Baseline age category: Baby Boomers. Baseline party: Democrats. Effects of candidate gender, generation, gender, prior voting, and county presidential vote share on Midterm Turnout in WA using weighted CCES Data. 2018 results among registered and weighted using registered voter weight. 2014 results among registered and weighted using year specific weight (registered voter weight unavailable for 2014).", 
          notes.append = TRUE, notes.align = "l", digits=3, single.row = T)

#drop WA models.
rm(m1, m2)

#PA 2018 General.
#use rvweight as before.
m1 <-
  glm(
    Voted ~  FemaleCandidate+   FemaleCandidate*Female+
      FemaleCandidate*GenZ+
      FemaleCandidate*Millennial+
      FemaleCandidate*GenX+
      FemaleCandidate*SilentGen+
      FemaleCandidate*Republican+
      FemaleCandidate*Independents+
      PriorVoting + Female + GenZ + Millennial +
      GenX + SilentGen+Republican+Independents+Proptrumpvote2016,
    weights = rvweight, family=binomial(link="logit"),
    data = cces_PA18
  )
summary(m1)
#Compute Effects of Female Candidate.
summary(margins(m1, variables=c("FemaleCandidate"))) #0.0033  CIs:-0.0272 0.0338  (n.s.)
summary(margins(m1, variables=c("FemaleCandidate"), at=list(Female=0:1)))


#PA 2014 General.
#Note: the rvweight (registered voter weight) is not available for 2014. 
#In lieu of this, we drop those who are either no record or unregistered to more closely approximate the turnout among registered data.
table(cces_PA14$vv_regstatus, exclude=NULL) #only want to keep those who are registered, so drop 2 and 3 (no record and unregistered)

m2 <-
  glm(
    Voted ~  FemaleCandidate+   FemaleCandidate*Female+
      FemaleCandidate*Millennial+
      FemaleCandidate*GenX+
      FemaleCandidate*SilentGen+
      FemaleCandidate*Republican+
      FemaleCandidate*Independents+
      PriorVoting + Female  + Millennial +
      GenX + SilentGen+Republican+Independents+obamavote2012,
    weights = weight, family=binomial(link="logit"),
    data =subset(cces_PA14, (cces_PA14$vv_regstatus!=2|cces_PA14$vv_regstatus!=3))
  )
summary(m2)


#Compute Effects of Female Candidate.
summary(margins(m2, variables=c("FemaleCandidate"))) #0.0061  CIs:-0.0304 0.0427  (n.s.)
summary(margins(m2, variables=c("FemaleCandidate"), at=list(Female=0:1)))

#Export model results to table (Appendix Table E.1).
stargazer(m1, m2, out="PAGeneralTurnoutAmongRegisteredCCESComparison.html",
          type="html",
          covariate.labels = c("Female Candidate", "Female", 
                               "Generation Z","Millennials", "Generation X","Silent Generation", 
                               "Republican", "Other Party",
                               "Prior Voting (2016/2012 General Election)", 
                               "County Trump Vote 2016","County Obama Vote 2012", 
                               "Female Candidate*Female",
                               "Female Candidate*Generation Z",
                               "Female Candidate*Millennials",
                               "Female Candidate*Generation X",
                               "Female Candidate*Silent Generation",
                               "Female Candidate*Republican",
                               "Female Candidate*Other Party"),
          dep.var.labels="", column.labels = c("Voted in 2018 Midterms", "Voted in 2014 Midterms"),
          title = "",
          notes        = "Results from logistic regression. Standard errors in parentheses. Baseline age category: Baby Boomers. Baseline party: Democrats. Effects of candidate gender, generation, gender,  party, prior voting, and county presidential vote share on Midterm Turnout in PA using weighted CCES Data. 2018 results among registered and weighted using registered voter weight. 2014 results among registered and weighted using year specific weight (registered voter weight unavailable for 2014).", 
          notes.append = TRUE, notes.align = "l", digits=3, single.row = T)

